<?php
/**
 * @package DBpretty
 * @author WiFeng
 * @link http://521-wf.com
 */

class DBpretty_Options_Export extends DBpretty_Options implements Widget_Interface_Do
{
	public $defaultFilename;
	public $db;
	
	public function execute() {
		$gmtTime = Typecho_Date::gmtTime();
		$date = new Typecho_Date($gmtTime);
		$this->defaultFilename = $date->format('YmdH').'_'.DBpretty_Options::random(7);
		$this->db = Typecho_Db::get();
	}
	
	public function form() {
		/** 构建表格 */
		$form = new Typecho_Widget_Helper_Form(Typecho_Common::url('/action/dbpretty-do', $this->options->index),
				Typecho_Widget_Helper_Form::POST_METHOD);
		
		$type = new Typecho_Widget_Helper_Form_Element_Radio('type', array('all' => _t('Typecho全部数据'), 'custom' => _t('自定义备份')), 'all', _t('数据备份类型'));
		$form->addInput($type);
		
		$type->addItem($this->getSelectAllCheckItem());
		$type->addItem($this->getTableNameItem());
		
		$sqlcompat = new Typecho_Widget_Helper_Form_Element_Radio('sqlcompat', array('default' => _t('默认'), 'MYSQL40' => _t('MySQL 3.23/4.0.x'), 'MYSQL41' => _t('MySQL 4.1.x/5.x')), 'default', _t('建表语句格式'));
		$form->addInput($sqlcompat);
		
		$usezip = new Typecho_Widget_Helper_Form_Element_Radio('usezip', array('0' => _t('否'), '1' => _t('是')), 0, _t('压缩备份文件'));
		$form->addInput($usezip);
		
		$filename = new Typecho_Widget_Helper_Form_Element_Text('filename', NULL, $this->defaultFilename, _t('备份文件名'), _t('这里只需输入文件名（不包括后缀名称），后缀名称程序将自动追加'));
		$form->addInput($filename);
		
		
		$operation = new Typecho_Widget_Helper_Form_Element_Hidden('operation', array(), 'export');
		$form->addItem($operation);
		
		/** 提交按钮 */
		
		$form->addItem($this->getSubmitButton());
		
		return $form;
	}
	
	public function action() {}
	
	private function getTableNameItem() {
		$allTables = DBpretty_Options::getAllTables();
		$outLiItem = new Typecho_Widget_Helper_Layout('li');
		$tablenameItem = new Typecho_Widget_Helper_Layout('div');
		$ulItem = new Typecho_Widget_Helper_Layout('ul');
		$inputName = 'tablename';
		
		foreach($allTables as $tablename) {
			$liItem = new Typecho_Widget_Helper_Layout('li');
			$inputItem = new Typecho_Widget_Helper_Layout('input');
			$labelItem = new Typecho_Widget_Helper_Layout('label');
			$labelItem->setAttribute('for', $inputName.'-'.$tablename)->html($tablename);
			$inputItem->setAttribute('name', $inputName.'[]')->setAttribute('id', $inputName.'-'.$tablename)->setAttribute('type', 'checkbox')->setAttribute('checked', 'checked')->setAttribute("value", $tablename);
			$liItem->setAttribute('style', 'padding-left:0px; margin: 5px 0; width:200px;');
			$liItem->addItem($inputItem);
			$liItem->addItem($labelItem);
			$ulItem->addItem($liItem);
		}
		$tablenameItem->addItem($ulItem);
		$outLiItem->addItem($tablenameItem);
		$outLiItem->setAttribute('style', 'display:none');
		return $outLiItem;
	}
	
	private function getSelectAllCheckItem() {
		$liItem = new Typecho_Widget_Helper_Layout('li');
		$inputItem = new Typecho_Widget_Helper_Layout('input');
		$labelItem = new Typecho_Widget_Helper_Layout('label');
		$labelItem->setAttribute('for', 'tablename-selectall')->html(_t('全选 - 数据表'));
		$inputItem->setAttribute('name', 'alltable')->setAttribute('id', 'tablename-selectall')->setAttribute('type', 'checkbox')->setAttribute('checked', 'checked');
		$liItem->addItem($inputItem)->addItem($labelItem);
		$liItem->setAttribute('style', 'display:none');
		return $liItem;
	}
	
	public function getSubmitButton() {
		$ulItem = new Typecho_Widget_Helper_Layout('ul');
		$liItem = new Typecho_Widget_Helper_Layout('li');
		$ulItem->addItem($liItem)->setAttribute('class', 'typecho-option');
		$inputItem = new Typecho_Widget_Helper_Layout('input');
		$inputItem->setAttribute('name', 'submit')->setAttribute('type', 'button')->setAttribute('value', _t('开始备份'))->setAttribute('onclick', 'exportDB(this);');
		$spanItem = new Typecho_Widget_Helper_Layout('span');
		$spanItem->setAttribute('id', 'dbpretty-export-msg');
		$liItem->addItem($inputItem);
		$liItem->addItem($spanItem);
		return $ulItem;
	}
}